<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/number.class.php');
    require_once($config['app_dir'] . '/includes/template.class.php');
    require_once($config['app_dir'] . '/classes/class.timezone.php');
    //---------------------------------------------------------------------------------------------------
    class objLastRateServer {
            protected $_data = false;
            protected $_sql_querys = array();
            public $userID;

        public function __construct() {
              $this->_sqls();
        }//constructor

        protected function Prepare() {
         	// get time offset depending on user timezone. Offset = 0 if user not logged in
         	$objTimezone = new objTimezone();
         	if (!$this->userID) $offset = 0;
         	else $offset = $objTimezone->getOffsetByUserId($this->userID);

          	$this->_data = objDataBase::GetInstance()->handler->GetAll($this->_sql_querys['rates'],array($offset));
            $this->BuildDecimal();
            return !empty($this->_data) && $this->_data;
        } //Prepare

        public function Render() {
            if( !$this->Prepare() ) return false;

            $Rates = new objTemplate();
            $Rates->Assign('data', $this->_data);
            return $Rates->Fetch('_RateItems.tpl');
        } //Render

        protected function BuildDecimal() {
            for($i=0; $i<count($this->_data); $i++) {
                $this->_data[$i]['decimal'] = NumberConvert::decimal($this->_data[$i]['PipSize']);
            }//for
        } //buildDecimal

        protected function _sqls() {
            $this->_sql_querys['rates'] =
                           "SELECT *, COALESCE(t.pipsize, 0.0001) as PipSize, FROM_UNIXTIME((r.`Time`+?), '%Y-%m-%d %H:%i:%s') as RateTime ".
                           " FROM fa_rates r ".
                              "LEFT OUTER join fa_trades t ON (t.TradeID=r.TradeID ) ".
                           " ORDER BY r.SortIndex ";
        } //_sqls

    } //class objLastRateServer

    //---------------------------------------------------------------------------------------------------
    function showLastRates($userID=false) {
        ob_start();
        $LastRateServer = new objLastRateServer();
        $LastRateServer->userID = $userID;
        //----------------------------------------------------------------------
        $objResponse = new xajaxResponse();
        $objResponse->addAssign("divRates","innerHTML", $LastRateServer->Render());
        //----------------------------------------------------------------------
        $errors = ob_get_contents();
        ob_end_clean();
        return $objResponse;
    }//showLastRates
//-------------------------------------------------------------------------------------------------------
?>
